package com.webviewscrolldemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.webviewscrolldemo.R;

public class VerticalSeekBar extends View {

	private final static String TAG = "VerticalSeekBar";
	private Context mContext;
	// 要画的图片

	//private Drawable mBackGroud;

	private Drawable mThumb;
	private int mThumbImage =-1;
	// 背景
	// 物理像素，屏幕上显示的宽度和高度
	private int m_nBackGroudWidth =0 ;
	private int m_nBackGroudHeight;

	// 滑块
	private int m_nThumbHeight = 50;
	private int m_nThumbWidth = 50;

	// 滚动条高度,当滑块在最低端时，这时进度应该是0，所以滚动条高度应该是背景高度减去滑块高度

	private int m_nSeekBarHeight;
	private int m_nThumbLeft; // 滑块里父元素左边的距
	private int m_nThumbTop; // 滑块离顶部距
	private int m_nProgress; // 当前的进度
	private int m_nMax = 100; // 最大值,一般从0开始

	private OnVerticalSeekBarChangeListener mListener;

	public interface OnVerticalSeekBarChangeListener {

		void onProgressChanged(VerticalSeekBar verticalSeekBar, int progress);

	}

	public VerticalSeekBar(Context context, AttributeSet attrs) {

		super(context, attrs);
		this.mContext = context;
	}

	@Override
	protected void onDraw(Canvas canvas) {

		// getLeft相对于父元素左边距离,getRight=getLeft+getWidth

		int width = getWidth();
		m_nBackGroudHeight = getHeight();
		if(m_nBackGroudWidth == 0){
			m_nBackGroudWidth = width;
		}
		//Log.i(TAG, "width=" + width+" hight = "+getHeight());
		
		//设置滑块背影
		if(mThumb ==null && mThumbImage == -1){
			mThumb = mContext.getResources()
					.getDrawable(R.drawable.ic_launcher);
		}else if(mThumb !=null && mThumbImage != -1){
			mThumb = mContext.getResources().getDrawable(mThumbImage);
		}
		
		m_nSeekBarHeight = m_nBackGroudHeight - m_nThumbHeight;
		m_nThumbLeft = m_nBackGroudWidth - m_nThumbWidth;
		//初始化的位置 顶部
		mThumb.setBounds(m_nThumbLeft, m_nThumbTop, m_nThumbLeft+ m_nThumbWidth, m_nThumbTop+ m_nThumbHeight);
		mThumb.draw(canvas);

	}
	
	//设置背影宽度
	public void setBackGroudWidth(int width){
		m_nBackGroudWidth = width;
	}
	
	/**
	 * 滑块的宽和高
	 * @param width
	 */
	public void setThumbWidth(int width){
		m_nThumbWidth = width;
	}
	
	public void setThumbHight(int hight){
		m_nThumbHeight = hight;
	}
	
	/**
	 * 设置滑块背影
	 * @param drawable
	 */
	public void setDrawable(Drawable drawable ){
			mThumb = drawable;
	}
	
	public void setDrawableResourcesID(int id){
		mThumbImage = id;
	}
	

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		int Y = (int) event.getY();

		int progress = 0;

		// 这是滑块已经到达顶部

		if (Y <= m_nThumbHeight / 2) {
			progress = m_nMax;
		}

		// 滑块到达最底端

		else if (Y >= (m_nSeekBarHeight + m_nThumbHeight / 2)) {
			progress = 0;
		} else {
			progress = (m_nSeekBarHeight - Y + m_nThumbHeight / 2) * m_nMax
					/ m_nSeekBarHeight;
		}
		refresh(progress);

		return true;

	}

	private void refresh(int progress) {
		m_nThumbTop = (m_nMax - progress) * m_nSeekBarHeight / m_nMax; // 把m_nSeekBarHeight分成m_nMax份，progress是当前的份额
		mThumb.setBounds(m_nThumbLeft, m_nThumbTop, m_nThumbLeft
				+ m_nThumbWidth, m_nThumbTop
		+ m_nThumbHeight);
		invalidate();
		if (mListener != null) {
			mListener.onProgressChanged(this, progress);
		}

	}

	public void setListener(OnVerticalSeekBarChangeListener a_Listen) {
		mListener = a_Listen;
	}

	public void setMax(int max) {
		this.m_nMax = max;
		refresh(0);
	}
	
	public int getMax(){
		return this.m_nMax;
	}
	
	/**
	 * 设置位置
	 * @param progress
	 */
	public void setLocation(int progress){
		if(progress > m_nMax){
			progress = m_nMax;
		}
		
		 refresh(progress);
	}

}